﻿using System;
using System.Windows.Forms;
using System.Data;
using System.IO;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmAdvanceSearch : Form
    {
        DialogResult dlgResult;
        string _No = "";
        string _CardNo = "";
        string _LCNo = "";
        string _IDCardNo = "";
        string _FullName = "";
        int _Standard_ID = 0;
        int _Position_ID = 0;
        int _ContractType_ID = 0;
        string _Specialist = "";
        decimal _BasicSalaryFrom;
        decimal _BasicSalaryTo;
        string _Address = "";
        string _Job= "";
        string _BookNo = "";
        string _MedicalNo = "";
        DateTime _StartDateTo = DateTime.MinValue;
        DateTime _StartDateFrom = DateTime.MinValue;
        int _Grade_ID = 0;
        int _Branch_ID;
        DateTime _DOBTo = DateTime.MinValue, _DOBFrom = DateTime.MinValue;
        string _POB = "";
        string _Sex = "";
        DateTime _LC_FinishDateTo = DateTime.MinValue, _LC_FinishDateFrom = DateTime.MinValue;
        DateTime _LC_BeginDateTo = DateTime.MinValue, _LC_BeginDateFrom = DateTime.MinValue;
        PositionClient wcfP;
        ContractTypeClient wcfCT;
        GradeClient wcfGrade;
        LevelClient wcfLevel;

        public string No
        {
            get { return _No; }
            set { _No = value; }
        }

        public string CardNo
        {
            get { return _CardNo; }
            set { _CardNo = value; }
        }


        public string IDCardNo
        {
            get { return _IDCardNo; }
            set { _IDCardNo = value; }
        }


        public string FullName
        {
            get { return _FullName; }
            set { _FullName = value; }
        }


        public DateTime DOBFrom
        {
            get { return _DOBFrom; }
            set { _DOBFrom = value; }
        }

        public DateTime DOBTo
        {
            get { return _DOBTo; }
            set { _DOBTo = value; }
        }

       

        public string POB
        {
            get { return _POB; }
            set { _POB = value; }
        }

        public string Sex
        {
            get { return _Sex; }
            set { _Sex = value; }
        }

        public int Branch_ID
        {
            get { return _Branch_ID; }
            set { _Branch_ID = value; }
        }

        public int Standard_ID
        {
            get { return _Standard_ID; }
            set { _Standard_ID = value; }
        }

        public int Position_ID
        {
            get { return _Position_ID; }
            set { _Position_ID = value; }
        }

        public int Grade_ID
        {
            get { return _Grade_ID; }
            set { _Grade_ID = value; }
        }

        public int ContractType_ID
        {
            get { return _ContractType_ID; }
            set { _ContractType_ID = value; }
        }

        public string LCNo
        {
            get { return _LCNo; }
            set { _LCNo = value; }
        }
        public string Specialist
        {
            get { return _Specialist; }
            set { _Specialist = value; }
        }

        public decimal BasicSalaryFrom
        {
            get { return _BasicSalaryFrom; }
            set { _BasicSalaryFrom = value; }
        }
        public decimal BasicSalaryTo
        {
            get { return _BasicSalaryTo; }
            set { _BasicSalaryTo = value; }
        }

        public DateTime LC_FinishDateFrom
        {
            get { return _LC_FinishDateFrom; }
            set { _LC_FinishDateFrom = value; }
        }
        public DateTime LC_FinishDateTo
        {
            get { return _LC_FinishDateTo; }
            set { _LC_FinishDateTo = value; }
        }
        public DateTime LC_BeginDateTo
        {
            get { return _LC_BeginDateTo; }
            set { _LC_BeginDateTo = value; }
        }
        public DateTime LC_BeginDateFrom
        {
            get { return _LC_BeginDateFrom; }
            set { _LC_BeginDateFrom= value; }
        }

       
        public DateTime StartDateTo
        {
            get { return _StartDateTo; }
            set { _StartDateTo = value; }
        }
        public DateTime StartDateFrom
        {
            get { return _StartDateFrom; }
            set { _StartDateFrom = value; }
        }
       
        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        public string Job
        {
            get { return _Job; }
            set { _Job = value; }
        }
        public string MedicalNo
        {
            get { return _MedicalNo; }
            set { _MedicalNo = value; }
        }
        public string BookNo
        {
            get { return _BookNo; }
            set { _BookNo = value; }
        }
        public frmAdvanceSearch()
        {
            InitializeComponent();
            dlgResult = DialogResult.Cancel;
            Language();
            LoadStandard();
            LoadPosition();
            LoadContractType();
            LoadGrade();
        }

        public frmAdvanceSearch(int branch_ID)
        {
            InitializeComponent();
            this.Branch_ID = branch_ID;
            dlgResult = DialogResult.Cancel;
            Language();
            LoadStandard();
            LoadPosition();
            LoadContractType();
            LoadGrade();
        }

        private void Language()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_Panel(ref panel2);
        }
        void LoadStandard()
        {
            StandardClient wcfStandard = Service.Standard();
            DataTable dt = wcfStandard.Standard_GetAll().Tables[0];
            DataRow dr = dt.NewRow();
            dr["Standard_ID"] = 0;
            dr["Standard_Name"] = "";
            dt.Rows.InsertAt(dr, 0);
            cboStandard.DataSource = null;
            cboStandard.DisplayMember = "Standard_Name";
            cboStandard.ValueMember = "Standard_ID";
            cboStandard.DataSource = dt;
        }


        void LoadContractType()
        {
            wcfCT = Service.ContractType();
            DataTable dt = wcfCT.ContractType_GetAll().Tables[0];
            

            DataRow dr = dt.NewRow();
            dr["ContractType_ID"] = 0;
            dr["ContractType_Name"] = "";
            dt.Rows.InsertAt(dr, 0);

            dt.DefaultView.RowFilter = "ContractType_Branch_ID = " + Branch_ID.ToString() + " OR ContractType_ID = 0";

            cboContract_Type.DataSource = null;
            cboContract_Type.DisplayMember = "ContractType_Name";
            cboContract_Type.ValueMember = "ContractType_ID";
            cboContract_Type.DataSource = dt;
        }
        void LoadPosition()
        {
            wcfP = Service.Position();
            //DataTable dt = wcfP.Position_GetAll().Tables[0];
            DataTable dt = wcfP.Position_GetByBranchID(_Branch_ID).Tables[0];
            DataRow dr = dt.NewRow();
            dr["Position_ID"] = 0;
            dr["Position_Name"] = "";
            dt.Rows.InsertAt(dr, 0);
            cboPosition.DataSource = null;
            cboPosition.DisplayMember = "Position_Name";
            cboPosition.ValueMember = "Position_ID";
            cboPosition.DataSource = dt;
        }

        void LoadGrade()
        {
            wcfGrade = new GradeClient(ProxyConfig.GetBinding(), ProxyConfig.Grade);
            //DataTable dt = wcfGrade.Grade_GetAll().Tables[0];
            DataTable dt = wcfGrade.Grade_GetByBranch(Branch_ID).Tables[0];
            DataRow dr = dt.NewRow();
            dr["Grade_ID"] = 0;
            dr["Grade_Name"] = "";
            dt.Rows.InsertAt(dr, 0);
            cboGrade.DataSource = null;
            cboGrade.DisplayMember = "Grade_Name";
            cboGrade.ValueMember = "Grade_ID";
            cboGrade.DataSource = dt;
        }


        private void frmAdvanceSearch_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Close();
            this.DialogResult = dlgResult;
        }

        private void frmAdvanceSearch_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            _No = txtEmpNo.Text;
            //_CardNo = txtCardNo.Text;
            _FullName = txtFullName.Text;
            _Standard_ID = Convert.ToInt32(cboStandard.SelectedValue);
            _Specialist = txtSpecialist.Text;
            _Position_ID = Convert.ToInt32(cboPosition.SelectedValue);
            _ContractType_ID = Convert.ToInt32(cboContract_Type.SelectedValue);
            _LCNo = txtLC_No.Text;
            _Grade_ID = Convert.ToInt32(cboGrade.SelectedValue);
            _BasicSalaryFrom = UISetting.GetDecimalNo(txtBasicSalaryFrom.Text);
            _BasicSalaryTo = UISetting.GetDecimalNo(txtBasicSalaryTo.Text);
            _Address = txtAddress.Text;
            _BookNo = txtBookNo.Text;
            _MedicalNo = txtMedicalNo.Text;
            _Job = txtJob.Text;

            if (Program.isAdmin || Program.isViewAll)
                _Branch_ID = 0;
            else
                _Branch_ID = Program.intBranchID;


            if (txtBasicSalaryFrom.Text.Length > 0)
                if (!CheckTextValid.IsDecimal(txtBasicSalaryFrom.Text))
                {
                    txtBasicSalaryFrom.Focus();
                    MessageBox.Show("Tiền lương cơ bản không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;

                }

            if (txtBasicSalaryFrom.Text.Length > 0)
                if (!CheckTextValid.IsDecimal(txtBasicSalaryTo.Text))
                {
                    txtBasicSalaryTo.Focus();
                    MessageBox.Show("Tiền lương cơ bản không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;

                }


            if (mtxDOBFrom.Text.Trim() != "/  /")
            {
                if (!CheckTextValid.IsDateTime(mtxDOBFrom.Text, "dd/MM/yyyy"))
                {
                    mtxDOBFrom.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngaysinhkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (mtxDOBTo.Text.Trim() != "/  /")
            {
                if (!CheckTextValid.IsDateTime(mtxDOBTo.Text, "dd/MM/yyyy"))
                {
                    mtxDOBTo.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngaysinhkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            _DOBFrom = UISetting.GetDate(mtxDOBFrom.Text);

            _DOBTo = UISetting.GetDate(mtxDOBTo.Text);

            _POB = txtPOB.Text;
            if (radMale.Checked)
                _Sex = "True";
            else if (radFemale.Checked)
                _Sex = "False";
            _IDCardNo = txtIDCardNo.Text;
            if (mtxLCFinishDateFrom.Text.Trim() != "/  /")
            {
                if (!CheckTextValid.IsDateTime(mtxLCFinishDateFrom.Text, "dd/MM/yyyy"))
                {
                    mtxLCFinishDateFrom.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (mtxLCFinishDateTo.Text.Trim() != "/  /")
            {
                if (!CheckTextValid.IsDateTime(mtxLCFinishDateTo.Text, "dd/MM/yyyy"))
                {
                    mtxLCFinishDateTo.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (mtxLCBeginDateFrom.Text.Trim() != "/  /")
            {
                if (!CheckTextValid.IsDateTime(mtxLCBeginDateFrom.Text, "dd/MM/yyyy"))
                {
                    mtxLCBeginDateFrom.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (mtxLCBeginDateTo.Text.Trim() != "/  /")
            {
                if (!CheckTextValid.IsDateTime(mtxLCBeginDateTo.Text, "dd/MM/yyyy"))
                {
                    mtxLCBeginDateTo.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            _LC_BeginDateFrom = UISetting.GetDate(mtxLCBeginDateFrom.Text);
            _LC_BeginDateTo = UISetting.GetDate(mtxLCBeginDateTo.Text);
            _LC_FinishDateFrom = UISetting.GetDate(mtxLCFinishDateFrom.Text);            
            _LC_FinishDateTo = UISetting.GetDate(mtxLCFinishDateTo.Text);
            _StartDateFrom = UISetting.GetDate(mtxStartDateFrom.Text);
            _StartDateTo = UISetting.GetDate(mtxStartDateTo.Text);  
           
            
            dlgResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }



    }
}